﻿/**
动力计划指标 JS 专用
**/
function viewModel() {
    var self = this;
    this.grid = {
        size: { w: 4, h: 40 },
        url: '/api/pacm/Dljhzb/getall',
        queryParams: ko.observable(),
        pagination: true,
        idField: 'Dljhzb_BillNo'
    };
    this.gridEdit = new com.editTreeGridViewModel(this.grid);
    this.refreshClick = function () {
        window.location.reload();
    };
    this.addClick = function () {
        com.ajax({
            type: 'GET',
            url: '/api/pacm/zzjhzb/GetNewBillNo',
            success: function (d) {
                if (self.grid.onClickRow()) {
                    var row = { Dljhzb_BillNo: d, Dljhzb_Pjdlqd: 0, Dljhzb_Zlxl: 0, Dljhzb_Fykzfw: 0 };
                    self.grid.treegrid('append', { data: [row] });
                    self.grid.treegrid('select', row.Dljhzb_BillNo);
                    self.grid.$element().data("datagrid").insertedRows.push(row);
                    self.editClick();
                }
            }
        });

    };
    this.editClick = function () {
        var row = self.grid.treegrid('getSelected');
        if (row) {
            //self.gridEdit.begin(row);

            //开始编辑行数据
            self.grid.treegrid('beginEdit', row.Dljhzb_BillNo);
            self.edit_id = row.Dljhzb_BillNo;
            var eds = self.grid.treegrid('getEditors', row.Dljhzb_BillNo);
            var edt = function (field) { return $.grep(eds, function (n) { return n.field == field })[0]; };
            self.afterCreateEditors(edt);
        }
    };
    //
    this.afterCreateEditors = function (editors) {
        //var iconInput = editors("Dljhzb_Dwid").target;
        //        var d = ("lookup", { lookupType: 'project', valueTitle: '项目编码', textTitle: '项目名称' });
        //        var onShowPanel = function () {
        //            iconInput.lookup('hidePanel');
        //            com.dialog({
        //                title: '&nbsp;切换项目',
        //                width: 600,
        //                height: 420,
        //                content: "<iframe id='frm_win_project' src='/plugins/lookup?r=" + Math.random() + "' style='height:100%;width:100%;border:0;' frameborder='0'></iframe>",
        //                viewModel: function (d) {
        //                    this.confirmClick = function () {
        //                        alert("ok");
        //                    };
        //                },
        //                closable: true,
        //                onClose: function () {
        //                }
        //            });
        //        };
        //        iconInput.lookup({ customShowPanel: true, onShowPanel: onShowPanel, editable: true });
        //        iconInput.lookup('resize', iconInput.parent().width());
        //        iconInput.lookup('textbox').unbind();




    };
    this.grid.OnBeforeDestroyEditor = function (editors, row) {
        //获取部门名称（显示值） 
        row.Dljhzb_Dwmc = editors["Dljhzb_Dwid"].target.lookup('textbox').val();
    };

    this.grid.OnAfterCreateEditor = function (edt) {
        com.readOnlyHandler('input')(edt["Dljhzb_BillNo"].target, true);
    };
    this.grid.onDblClickRow = self.editClick;
    this.grid.onClickRow = function () {
        var edit_id = self.edit_id;
        if (!!edit_id) {
            if (self.grid.treegrid('validateRow', edit_id)) { //通过验证
                self.grid.treegrid('endEdit', edit_id);
                self.edit_id = undefined;
            }
            else { //未通过验证
                self.grid.treegrid('select', edit_id);
                return false;
            }
        }
        return true;
    };

    //审核
    this.auditClick = function () {
        var row = self.grid.treegrid('getSelected');
        if (!row) return com.message('warning', "请选择一条明细数据!");
        com.auditDialog(function (d) {
            com.ajax({
                type: 'POST',
                url: "/api/pacm/Dljhzb/audit/" + row["Dljhzb_BillNo"],
                data: JSON.stringify(d),
                success: function () {
                    com.message('success', "单据已通过审核");
                }
            });
        });
    };
    this.deleteClick = function () {
        var row = self.grid.treegrid('getSelected');
        if (row) {
            self.grid.$element().treegrid('remove', row.Dljhzb_BillNo);
            self.grid.$element().data("datagrid").deletedRows.push(row);
        }
    };
    this.saveClick = function () {
        //  self.gridEdit.ended();
        self.grid.onClickRow();
        var post = {};
        post.list = self.gridEdit.getChanges(['Dljhzb_BillNo', 'Dljhzb_Year', 'Dljhzb_Month', 'Dljhzb_Dwid', 'Dljhzb_Dwmc', 'Dljhzb_Pjdlqd', 'Dljhzb_Zlxl', 'Dljhzb_Fykzfw', 'Dljhzb_Remark']);
        if (self.gridEdit.isChangedAndValid) {
            com.ajax({
                url: '/api/pacm/Dljhzb/edit',
                data: ko.toJSON(post),
                success: function (d) {
                    com.message('success', '保存成功！');
                    //self.grid.queryParams({ CodeType: self.CodeType() });
                    self.gridEdit.accept();
                }
            });
        }
    };
}